package luke4fun.wheresmymoney.panel;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout.LayoutParams;

import java.util.List;

import luke4fun.wheresmymoney.bean.SummaryBean;

public class StatisticsChartPanel extends View {
	
	private List<SummaryBean> dataList;
    RectF oval = new RectF(20, 20, 300, 300);
    Paint paint = new Paint();
	
	public void setDataList(List<SummaryBean> dataList) {
		this.dataList = dataList;
	}

	public StatisticsChartPanel(Context context) {
		super(context);
	}
	
	public StatisticsChartPanel(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@Override
	protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
		int width = 320;
	    if(getLayoutParams().width == LayoutParams.WRAP_CONTENT)
	    {
	    }
	    else if((getLayoutParams().width==LayoutParams.MATCH_PARENT))
	    {
	        width = MeasureSpec.getSize(widthMeasureSpec);
	    }
	    else
	        width = getLayoutParams().width;


	    int height = 320;
	    if(getLayoutParams().height==LayoutParams.WRAP_CONTENT)
	    {
	    }
	    else if((getLayoutParams().height==LayoutParams.MATCH_PARENT))
	    {
	        height = MeasureSpec.getSize(heightMeasureSpec);
	    }
	    else
	        height = getLayoutParams().height;


	    setMeasuredDimension(width|MeasureSpec.EXACTLY, height|MeasureSpec.EXACTLY);
	}

	@Override
    public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		if (dataList != null && dataList.size() > 0) {
	        paint.setAntiAlias(true);
	        paint.setStrokeWidth(3);
	        
	        float startAngel = 0;
	        float sweepAngel = 0;
	        for (int i=0; i<dataList.size(); i++) {
	        	SummaryBean bean = dataList.get(i);
	        	
	        	// draw stroke
	        	paint.setStyle(Paint.Style.STROKE);
	        	paint.setColor(Color.WHITE);
	        	
		        sweepAngel = bean.getPercentage() / 100 * 360;
		        canvas.drawArc(oval, startAngel, sweepAngel, true, paint);
	        	
		        // fill color
		        paint.setStyle(Paint.Style.FILL);
		        paint.setColor(bean.getColor());
		        
		        canvas.drawArc(oval, startAngel, sweepAngel, true, paint);
		        
		        startAngel += sweepAngel;
	        }
		}
    }
}
